#include <stdio.h>
#include <stdlib.h>
#include "../heads/Stack.h"

Stack createStack() {
    return createLinkedList();
}

void pushStack(Stack *stack, int data) {
    addToListHead(stack, data); // 栈的push操作等同于链表的头部插入
    stack->size++;
}

int popStack(Stack *stack) {
    if (isStackEmpty(stack)) {
        fprintf(stderr, "栈空异常！\n");
        exit(EXIT_FAILURE);
    }
    int data = stack->head->next->data;
    removeFromList(stack, data);
    return data;
}

int topStack(Stack *stack) {
    if (isStackEmpty(stack)) {
        fprintf(stderr, "栈空异常！\n");
        exit(EXIT_FAILURE);
    }
    return stack->head->next->data;
}

int isStackEmpty(Stack *stack) {
    return stack->head->next == NULL;
}

void destroyStack(Stack *stack) {
    destroyList(stack);
}
